package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.setting.eneity.User;
import com.bjpowernode.crm.util.DateTimeUtil;
import com.bjpowernode.crm.util.UUIDUtil;
import com.bjpowernode.crm.vo.PageinationVO;
import com.bjpowernode.crm.workbench.eneity.*;
import com.bjpowernode.crm.workbench.exception.ActivityException;
import com.bjpowernode.crm.workbench.exception.ClueException;
import com.bjpowernode.crm.workbench.service.ActivityService;
import com.bjpowernode.crm.workbench.service.ClueActivityRelationService;
import com.bjpowernode.crm.workbench.service.ClueService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ClueController {
    @Autowired
    private ClueService clueService;
    @Autowired
    private ClueActivityRelationService relation;
    @Autowired
    private ActivityService activityService;
    @RequestMapping(value = "/workbench/clue/searchUsers.do")
    @ResponseBody
    public List<User> searchUsers(){
        //调用service层进行查询
        List<User> userList=clueService.searchUsers();
        return userList;
    }
    @RequestMapping("/workbench/clue/insert.do")
    @ResponseBody
    public Map<String,Boolean> insertClue(HttpServletRequest request,Clue clue){
        //创建集合
        Map<String, Boolean> map=new HashMap<>();
        //完善请求参数
        clue.setId(UUIDUtil.getUUID());
        HttpSession session=request.getSession(false);
        User user= (User) session.getAttribute("loginUser");
        clue.setCreateBy(user.getName());
        clue.setCreateTime(DateTimeUtil.getSysTime());
        //调用service层
        Boolean success=null;
        try{
             success=clueService.insert(clue);
        }catch(ClueException cx){
           cx.printStackTrace();
        }finally {
            map.put("success",success);
            return map;
        }

    }
    @RequestMapping(value = "/workbench/clue/pageList.do")
    @ResponseBody
    public PageinationVO<Clue> pageList(@RequestParam Map<String,String> clueMap){
        //完善请求参数
        String pageNo=clueMap.get("pageNo");
        if (pageNo==null||"".equals(pageNo)){
            pageNo="1";
        }
        clueMap.put("pageNo",pageNo);
        //调用service层
        PageinationVO<Clue> vo= clueService.pageList(clueMap);
        return vo;
    }
    //线索删除
    @RequestMapping(value = "/workbench/clue/delete.do")
    @ResponseBody
    public Map<String,Boolean> deleteClue(HttpServletRequest request){
        //获取请求参数id值
        String[] ids=request.getParameterValues("id");
        //调用service层
        Boolean success=null;
        Map<String,Boolean> map=new HashMap<>();
        try{
            success=clueService.delete(ids);
        }catch (ClueException ex){
            ex.printStackTrace();
        }finally {
            map.put("success",success);
            return map;
        }
    }
    //详细信息展示
    @RequestMapping(value = "/workbench/clue/detail.do")
    @ResponseBody
    public ModelAndView detail(String id){
        //调用service
        Clue clue=clueService.detail(id);
        //进行视图转发
        ModelAndView model=new ModelAndView();
        model.addObject("clue",clue);
        model.setViewName("forward:/workbench/clue/detail.jsp");
        return model;
    }
    //获取对应的备注信息
    @RequestMapping(value = "/workbench/clue/getRemarks.do")
    @ResponseBody
    public List<ClueRemark> getRemarks(String id){
        //调用service
        List<ClueRemark> remarkList=clueService.getRemarks(id);
        return remarkList;
    }
    //获取所关联的市场活动列表
    @RequestMapping(value = "/workbench/clue/getActivityList.do")
    @ResponseBody
    public List<Activity> getActivityList(String clueId){
        //调用service层
        List<Activity> activityList=clueService.getActivityList(clueId);
        return activityList;
    }
    //删除关联的市场活动记录
    @RequestMapping(value = "/workbench/clue/delRelation.do")
    @ResponseBody
    public Map<String,Boolean> delRelation(String id){
        //调用service层，relation
        Boolean success=null;
        Map<String,Boolean> map=new HashMap<>();
        try{
            success=relation.delRelation(id);
        }catch (ClueException ex){
            ex.printStackTrace();
        }finally {
            map.put("success",success);
            return map;
        }
    }
    //查询可关联的所有的市场活动
    @RequestMapping(value = "/workbench/clue/getAllActivity.do")
    @ResponseBody
    public List<Activity> getAllActivity(String clueId){
        //调用service,市场活动的service
        List<Activity> activityList=activityService.getAllActivity(clueId);
        return activityList;
    }
    //根据name搜索可以关联的市场活动信息
    @RequestMapping(value = "/workbench/clue/getAllActivityById.do")
    @ResponseBody
    public List<Activity> getAllActivityById(@RequestParam String clueId,@RequestParam String name){
        //将参数封装成一个map
        Map<String,String> paramMap=new HashMap<>();
        paramMap.put("clueId",clueId);
        paramMap.put("name",name);
        //调用activity的service层
        List<Activity> activityList=activityService.getAllActivityByIdAndName(paramMap);
        return activityList;
    }
    //添加关联的市场活动列表
    @RequestMapping(value ="/workbench/clue/insertActivity.do" )
    @ResponseBody
    public Map<String,Boolean> insertActivity(HttpServletRequest request){
        String[] activityIds=request.getParameterValues("activityId");
        String[] clueIds=request.getParameterValues("clueId");
        //将要添加的对象封装到一个集合中
        List<ClueActivityRelation> relationList=new ArrayList<>();
        for (int i=0;i<activityIds.length;i++){
            ClueActivityRelation clueActivityRelation=new ClueActivityRelation();
            clueActivityRelation.setId(UUIDUtil.getUUID());
            clueActivityRelation.setClueId(clueIds[i]);
            clueActivityRelation.setActivityId(activityIds[i]);
            relationList.add(clueActivityRelation);
        }
        //调用service
        Map<String,Boolean> map=new HashMap<>();
        Boolean success=null;
        try{
            success=relation.insertActivityByIds(relationList);
        }catch (ClueException ex){
            ex.printStackTrace();
        }finally {
            map.put("success",success);
            return map;
        }
    }
    @RequestMapping(value = "/workbench/clue/getActivityListByClue.do")
    @ResponseBody
    public List<Activity> getActivityList(){
        //调用service层
        List<Activity> activityList=activityService.getActivityList();
        return activityList;
    }
    @RequestMapping(value = "/workbench/clue/getActivityListByName.do")
    @ResponseBody
    public List<Activity> getActivityByName(String name){
        //调用service
        List<Activity> activityList=activityService.getActivityByName(name);
        return activityList;
    }
    @RequestMapping(value = "/workbench/clue/convert.do")
    @ResponseBody
    public ModelAndView convert(@RequestParam Map<String,String> params,HttpServletRequest request){
        //根据传递的参数判断是否要创建交易记录
        Tran tran=null;
        HttpSession session=request.getSession(false);
        User user= (User) session.getAttribute("loginUser");
        String creatBy=user.getName();
        if ("1".equals(params.get("flag"))){
            //创建交易记录
            tran=new Tran();
            tran.setActivityId(params.get("activityId"));
            tran.setMoney(params.get("money"));
            tran.setName(params.get("name"));
            tran.setExpectedDate(params.get("expectedDate"));
            tran.setStage(params.get("stage"));
            tran.setCreateBy(creatBy);
            tran.setCreateTime(DateTimeUtil.getSysTime());
            tran.setId(UUIDUtil.getUUID());
        }
        //如果没走if，则不需要创建交易，直接调用service
        Boolean success=null;
        ModelAndView model=null;
        try{
            success=clueService.convert(tran,params.get("clueId"),creatBy);
            //重定向视图
            model=new ModelAndView();
            model.setViewName("redirect:/workbench/clue/index.jsp");
        }catch (ClueException ex){
            ex.printStackTrace();
        }
        return model;
    }
}
